/**
 * Created by ll on 2017/11/28.
 */

var kzTimeArray = new Array();
var rpyTimeArray = new Array();
var xcTimeArray = new Array();
var zwrpyTimeArray = new Array();
var linkdate1 = new Array();
var linkdate2 = new Array();
var linkdate3 = new Array();
$(function () {
    // 变色特效
    var backgroundElement = window.top.cfgetTopBackgroundElement();
    $(backgroundElement).css("background","rgba(225,115,95,0.85)");

    $('#equipmentId').combobox({									        // 设备组合框
        url: 'equipment/getAllEquip',
        valueField: 'value',                                            // 值为设备id
        textField: 'text',                                              // 内容为设备名
        panelHeight: 'auto',
        editable: false,
        onChange: function (newValue, oldValue) {// 组合框变化响应函
            $.ajax({
                cache: false,
                url: "timeMachining/getAllData",			// 根据设备和工况类型来获取日期
                data: {equipmentId:newValue},
                type: "POST",
                dataType: "json",
                success: function (result) {
                    // 将通道信息加载到通道组合框
                    kzTimeArray.splice(0,kzTimeArray.length);
                    rpyTimeArray.splice(0,rpyTimeArray.length);
                    xcTimeArray.splice(0,xcTimeArray.length);
                    console.log(result);
                    for (var i = 0; i < result.length; i++) {
                        if(result[i].work == "空转"){
                            kzTimeArray.push(result[i].date+"&"+result[i].time);
                        }
                        if(result[i].work =="静态热漂移"){
                            rpyTimeArray.push(result[i].date+"&"+result[i].time);
                        }
                        if(result[i].work =="铣槽"){
                            xcTimeArray.push(result[i].date+"&"+result[i].time);
                        } if(result[i].work =="静态晚7到早7"){
                            zwrpyTimeArray.push(result[i].date+"&"+result[i].time)
                        }
                    }
                    console.log(kzTimeArray);
                    console.log(rpyTimeArray);
                    console.log(xcTimeArray);
                    addArrayToTab(1,kzTimeArray);
                    addArrayToTab(2,rpyTimeArray);
                    addArrayToTab(3,xcTimeArray);
                }
            });
        }
    });
});

function addArrayToTab(type_num,data_array) {
    // 将现在的表内容存储并清空
    var oTabNow = document.getElementById('tab_00' + type_num.toString());
     var rowNumsNow = oTabNow.rows.length;
    for (i=0;i<rowNumsNow;i++)
    {
        oTabNow.deleteRow(i);
        rowNumsNow=rowNumsNow-1;
        i=i-1;
    }
    for (var i = 0; i < data_array.length; i++) {
         var dateArr = data_array[i].split("&");
        var rowObj = oTabNow.insertRow(oTabNow.rows.length); // 添加一行
        rowObj.insertCell(0).innerHTML = oTabNow.rows.length;
        rowObj.insertCell(1).innerHTML = dateArr[0];
        rowObj.insertCell(2).innerHTML = dateArr[1];
        rowObj.insertCell(3).innerHTML ="";
        rowObj.insertCell(4).innerHTML = '<a>下载</a>';

        var fffffffffff = rowObj.getElementsByTagName('a');
        // 下载操作响应事件
        rowObj.getElementsByTagName('a')[0].onclick = function () {
            var temprowid = this.parentNode.parentNode.rowIndex-1;
            var tempid = this.parentNode.parentNode.parentNode.id;
            var tableid = tempid.split("_00")[1];
            gendownloadlink(tableid,data_array,temprowid)
        }

    }
}
function formatOnlyDate(value){
    if(value==null){
        return null;
    }
    var date = new Date(value);
    var year = date.getFullYear().toString();
    var month = (date.getMonth() + 1);
    var day = date.getDate().toString();
    if (month < 10) {
        month = "0" + month;
    }
    if (day < 10) {
        day = "0" + day;
    }
    return year + "-" + month + "-" + day;
}
function gendownloadlink(type_num1,data_array1,row_id) {
    var dateArr1 = data_array1[row_id].split("&");
    // 要下载的日期
    var tempStr = dateArr1[0].split('-');
    var machineDateStr = tempStr[0].substring(2,4)+tempStr[1]+tempStr[2];
    if(machineDateStr=="") {alert("加工日期不能为空！");return;}
    // 要下载的时间段
    var tempStr = dateArr1[1].split('-');
    if(tempStr=="") {alert("加工时间段不能为空！");return;}
    var machineProcessStr = tempStr[0].split(':');
    // 要下载的起止时间
    var machineStartTimeStr = machineProcessStr[0]+machineProcessStr[1]+machineProcessStr[2];
    var machineProcessStr = tempStr[1].split(':');
    var machineEndTimeStr = machineProcessStr[0]+machineProcessStr[1]+machineProcessStr[2];
    console.log(machineDateStr+machineStartTimeStr+machineEndTimeStr);
    // 下载目标字符串
    var stringArrayTemp="001-SDT00-00,001-SDT00-01,001-SDT00-02,001-SDT00-03,001-SDT00-04,001-SDT00-05,001-SDT00-06,001-SDT00-07,001-SDT01-00,001-SDT01-01,001-SDT01-02,001-SDT01-03,001-SDT01-04,001-SDT01-05,001-SDT01-06,001-SDT01-07,001-SDT02-00,001-SDT02-01,001-SDT02-02,001-SDT02-03,001-SDT02-04,001-SDT02-05,001-SDT02-06,001-SDT02-07,001-SDT03-00,001-SDT03-01,001-SDT03-02,001-SDT03-03,001-SDT03-04,001-SDT03-05,001-SDT03-06,001-SDT03-07,001-SDT04-00,001-SDT04-01,001-SDT04-02,001-SDT04-03,001-SDT04-04,001-SDT04-05,001-SDT04-06,001-SDT04-07,001-SDT05-00,001-SDT05-01,001-SDT05-02,001-SDT05-03,001-SDT05-04,001-SDT05-05,001-SDT05-06,001-SDT05-07,001-SDT06-00,001-SDT06-01,001-SDT06-02,001-SDT06-03@001-SFT00-00,001-SFT00-01,001-SFT00-02,001-SFT00-03,001-SFT00-04,001-SFT00-05,001-SFT00-06,001-SFT00-07,001-SFT00-08,001-SFT00-09,001-SFT00-10,001-SFT00-11,001-SFT00-12,001-SFT00-13,001-SFT00-14,001-SFT01-00,001-SFT01-01,001-SFT01-02,001-SFT01-03,001-SFT01-04,001-SFT01-05,001-SFT01-06,001-SFT01-07,001-SFT02-00,001-SFT02-01,001-SFT02-02,001-SFT02-03,001-SFT02-04,001-SFT02-05,001-SFT02-06,001-SFT02-07,001-SFT02-08,001-SFT02-09,001-SFT02-10,001-SFT02-11,001-SFT02-12,001-SFT02-13,001-SFT02-14,001-SFT03-00,001-SFT03-01,001-SFT03-02,001-SFT03-03,001-SFT03-04,001-SFT03-05,001-SFT03-06,001-SFT03-07,001-SFT03-08,001-SFT03-09,001-SFT03-10,001-SFT03-11,001-SFT03-12,001-SFT03-13,001-SFT03-14,001-SFT04-00,001-SFT04-01,001-SFT04-02,001-SFT04-03,001-SFT04-04,001-SFT04-05,001-SFT04-06,001-SFT04-07,001-SFT04-08,001-SFT04-09,001-SFT04-10,001-SFT04-11,001-SFT04-12,001-SFT04-13,001-SFT04-14,001-SFT05-00,001-SFT05-01,001-SFT05-02,001-SFT05-03,001-SFT05-04,001-SFT05-05,001-SFT05-06,001-SFT05-07,001-SFT05-08,001-SFT05-09,001-SFT05-10,001-SFT05-11,001-SFT05-12,001-SFT05-13,001-SFT05-14,001-SFT06-00,001-SFT06-01,001-SFT06-02,001-SFT06-03,001-SFT06-04,001-SFT06-05,001-SFT06-06,001-SFT07-00,001-SFT07-01,001-SFT07-02,001-SFT07-03,001-SFT07-04,001-SFT07-05,001-SFT07-06,001-SFT07-07,001-SFT07-08,001-SFT07-09,001-SFT07-10,001-SFT07-11,001-SFT07-12,001-SFT07-13,001-SFT07-14,001-SFT08-00,001-SFT08-01,001-SFT08-02,001-SFT08-03,001-SFT08-04,001-SFT08-05,001-SFT08-06,001-SFT08-07,001-SFT09-00,001-SFT09-01,001-SFT09-02,001-SFT09-03,001-SFT09-04,001-SFT09-05,001-SFT09-06,001-SFT10-00,001-SFT10-01,001-SFT10-02,001-SFT10-03,001-SFT10-04,001-SFT10-05,001-SFT10-06,001-SFT10-07,001-SFT10-08,001-SFT10-09,001-SFT10-10,001-SFT10-11,001-SFT10-12,001-SFT10-13,001-SFT10-14,001-SFT11-00,001-SFT11-01,001-SFT11-02,001-SFT11-03,001-SFT11-04,001-SFT11-05,001-SFT11-06,001-SFT11-07,001-SFT11-08,001-SFT11-09,001-SFT11-10,001-SFT11-11,001-SFT11-12,001-SFT11-13,001-SFT11-14,001-SFT12-00,001-SFT12-01,001-SFT12-02,001-SFT12-03,001-SFT12-04,001-SFT12-05,001-SFT12-06,001-SFT12-07,001-SFT12-08,001-SFT12-09,001-SFT12-10,001-SFT12-11,001-SFT12-12,001-SFT12-13,001-SFT12-14,001-SFT13-00,001-SFT13-01,001-SFT13-02,001-SFT13-03,001-SFT13-04,001-SFT13-05,001-SFT13-06,001-SFT14-00,001-SFT14-01,001-SFT14-02,001-SFT14-03,001-SFT14-04,001-SFT14-05,001-SFT14-06,001-SFT14-07,001-SFT15-00,001-SFT15-01,001-SFT15-02,001-SFT15-03,001-SFT16-00,001-SFT16-01,001-SFT16-02,001-SFT16-03,001-SFT17-00,001-SFT17-01,001-SFT17-02,001-SFT17-03,001-SFT18-00,001-SFT18-01,001-SFT18-02,001-SFT18-03,001-SFT18-04,001-SFT18-05,001-SFT18-06,001-SFT18-07,001-SFT18-08,001-SFT18-09,001-SFT19-00,001-SFT19-01,001-SFT19-02,001-SFT19-03,001-SFT19-04,001-SFT19-05,001-SFT19-06,001-SFT19-07,001-SFT19-08,001-SFT19-09,001-SFT20-00,001-SFT20-01,001-SFT20-02,001-SFT20-03,001-SFT20-04,001-SFT20-05,001-SFT20-06,001-SFT21-00,001-SFT21-01,001-SFT21-02,001-SFT21-03,001-SFT21-04,001-SFT21-05,001-SFT21-06,001-SFT21-07,001-SFT21-08,001-SFT21-09,001-SFT22-00,001-SFT22-01,001-SFT22-02,001-SFT22-03,001-SFT23-00,001-SFT23-01,001-SFT23-02,001-SFT23-03,001-SFT23-04,001-SFT23-05,001-SFT23-06,001-SFT23-07,001-SFT23-08,001-SFT23-09,001-SFT24-00,001-SFT24-01,001-SFT24-02,001-SFT24-03,001-SFT24-04,001-SFT24-05,001-SFT24-06,001-SFT24-07,001-SFT24-08,001-SFT24-09,001-SFT25-00,001-SFT25-01,001-SFT25-02,001-SFT25-03,001-SFT26-00,001-SFT26-01,001-SFT26-02,001-SFT26-03,001-SFT26-04,001-SFT26-05,001-SFT26-06,001-SFT26-07,001-SFT26-08,001-SFT27-00,001-SFT27-01,001-SFT27-02,001-SFT27-03,001-SFT27-04,001-SFT27-05,001-SFT27-06,001-SFT27-07,001-SFT27-08,001-SFT27-09,001-SFT28-00,001-SFT28-01,001-SFT28-02,001-SFT28-03,001-SFT28-04,001-SFT28-05,001-SFT28-06,001-SFT28-07,001-SFT28-08,001-SFT28-09,001-SFT29-00,001-SFT29-01,001-SFT29-02,001-SFT29-03,001-SFT29-04,001-SFT29-05,001-SFT29-06,001-SFT29-07,001-SFT29-08,001-SFT29-09,001-SFT30-00,001-SFT30-01,001-SFT30-02,001-SFT30-03,001-SFT30-04,001-SFT30-05,001-SFT30-06,001-SFT30-07,001-SFT30-08,001-SFT30-09,001-SFT31-00,001-SFT31-01,001-SFT31-02@001-SCD-00,001-SCD-01,001-SCD-02@001-SET00-00,001-SET00-01,001-SET01-00,001-SET01-01@001-SCN-XPOS,001-SCN-YPOS,001-SCN-ZPOS,001-SCN-MAPOS,001-SCN-XPWR,001-SCN-YPWR,001-SCN-ZPWR,001-SCN-MAPWR,001-SCN-XFU,001-SCN-YFU,001-SCN-ZFU,001-SCN-MAFU,001-SCN-FR,001-SCN-SP,001-SCN-PA@";
    window.top.showCFlowerMask();
    $.ajax({
        cache: false,
        url: "historysearch/combine2csvzero",			// 根据设备和工况类型以及日期来获取加工时间
        data: {date:machineDateStr,start:machineStartTimeStr,end:machineEndTimeStr,pattern:stringArrayTemp},
        type: "POST",
        dataType: "json",
        success: function (result) {
            window.top.hideCFlowerMask();
            if(result.success.toString()=="true"){
                console.log(result);
                var webLink = result.msg;
                var webFileName = result.msg.split("combine/")[1];
                downloadFile(webLink);
            }
            else {
                alert("生成数据文件失败！请稍候再试！")
            }
        },
        error: function(result){
            window.top.hideCFlowerMask();
            alert("生成数据文件失败！请稍候再试！");
        }
    });

    console.log(stringArrayTemp);
}
function downloadFile(url) {
    try {
        var elemIF = document.createElement("iframe");
        elemIF.src = url;
        elemIF.style.display = "none";
        document.body.appendChild(elemIF);
    } catch (e) {
    }
}
